package com.ztgf.sys.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.sys.common.config.YmlConfig;
import com.ztgf.sys.mapper.SysAreaMapper;
import com.ztgf.sys.models.bo.SysAreaSaveBO;
import com.ztgf.sys.models.dto.SysAreaDTO;
import com.ztgf.sys.models.service.ISysAreaService;
import com.ztgf.sys.models.vo.SysAreaVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 区域  服务实现类
 * </p>
 *
 * @author YUE
 * @since 2020-05-25
 */
@DubboService
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysAreaDTO> implements ISysAreaService {
    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private SysAreaMapper sysAreaMapper;

    @Override
    public boolean add(SysAreaSaveBO sysAreaSaveBO) {
        SysAreaDTO sysAreaDTO = new SysAreaDTO();
        BeanUtils.copyProperties(sysAreaSaveBO,sysAreaDTO);
        Long id = ymlConfig.getPkId();
        sysAreaDTO.setId(id);
        return this.insert(sysAreaDTO);
    }

    @Override
    public List<String> selectAreaNameBatchIds(List<String> ids){
        List<SysAreaDTO> sysAreaDTOList = sysAreaMapper.selectBatchIds(ids);
        List<String> areaNameList = new ArrayList<>();
        for (SysAreaDTO sysAreaDTO : sysAreaDTOList) {
            areaNameList.add(sysAreaDTO.getName());
        }
        return areaNameList;
    }

    /**
     * 获取一级Id
     * @param id
     * @return
     */
    public SysAreaDTO getAreaStair(Long id){
        SysAreaDTO areaDTO = selectById(id);
        if(null == areaDTO){
            throw new RuntimeException("没有此区域Id");
        }
        if(areaDTO.getParentId() == 0){
            return areaDTO;
        }
        return getAreaStair(areaDTO.getParentId());
    }

    /**
     * 获取所有的省
     * @return
     */
    @Override
    public List<SysAreaDTO> getAllProvince(){
        Wrapper<SysAreaDTO> wrapper = new EntityWrapper();
        wrapper.eq("type", 1);
        return sysAreaMapper.selectList(wrapper);
    }

    /**
     * 获取完整的区域根据Id
     * @param id
     * @return
     */
    @Override
    public StringBuffer getParentArea(Long id,StringBuffer stringBuffer){
        if(0 == id)
            return stringBuffer;
        SysAreaDTO areaDTO = selectById(id);
        if(null == areaDTO)
            throw new RuntimeException("没有此区域Id");
        getParentArea(areaDTO.getParentId(),stringBuffer);
        stringBuffer.append(areaDTO.getName());
        return stringBuffer;
    }

    @Override
    public List<SysAreaDTO> getInfoByCityId(Long id) {
        return sysAreaMapper.getInfoByCityId(id);
    }

    /**
     * 生成区域树返回
     * @return
     */
    @Override
    public List<SysAreaVO> getAreaTree(){
        return builTree(baseMapper.selectListArea());
    }



    /**
     * 生成区域树(只到市)返回
     * @return
     */
    @Override
    public List<SysAreaVO> queryAreaTreeInCityStopList(){
        return builTree(baseMapper.selectListToCityStopArea());
    }

//    /**
//     * 获取所有的区域信息
//     * @return
//     */
//    public List<SysAreaDTO> getAllSysAreaList(){
//        Wrapper<SysAreaDTO> wrapper = new EntityWrapper<>();
//        wrapper.eq("status", SYSCodeEnum.STATUS_0);
//        wrapper.ne("parent_id", -1);//不查询中国人民共和国
//        return sysAreaMapper.selectList(wrapper);
//    }


    //建立树形结构
    private List<SysAreaVO> builTree(List<SysAreaVO> areaList){
        List<SysAreaVO> treeMenus =new  ArrayList<>();
        for(SysAreaVO menuNode : getRootNode(areaList)) {
            menuNode=buildChilTree(menuNode,areaList);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private SysAreaVO buildChilTree(SysAreaVO pNode,List<SysAreaVO> areaList){
        List<SysAreaVO> chilMenus =new ArrayList<>();
        for(SysAreaVO menuNode : areaList) {
            if(menuNode.getParentId().equals(pNode.getId())) {
                chilMenus.add(buildChilTree(menuNode,areaList));
            }
        }
        pNode.setSysAreaVOList(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<SysAreaVO> getRootNode(List<SysAreaVO> areaList) {
        List<SysAreaVO> rootMenuLists =new  ArrayList<>();
        for(SysAreaVO menuNode : areaList) {
            if(menuNode.getParentId().intValue() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }

}
